package com.company.game01;

import cn.hutool.core.util.RandomUtil;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.util.List;

public class MyUtil {

    public static final int SCREEN_WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
    public static final int SCREEN_HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
    //    public static final int FRAME_WIDTH = (int) (SCREEN_WIDTH * 0.75);
    //    public static final int FRAME_HEIGHT = (int) (SCREEN_HEIGHT * 0.8);
    public static final int FRAME_WIDTH = 960;
    public static final int FRAME_HEIGHT = 720;

    public static final int GAME_STATE_MENU = 0;
    public static final int GAME_STATE_RUN = 1;
    public static final int GAME_STATE_RESUME = 2;
    public static final int GAME_STATE_HELP = 3;
    public static final int GAME_STATE_ABOUT = 4;
    public static final int GAME_STATE_EXIT = 5;
    public static final int GAME_STATE_LOST = 6;
    public static final int GAME_STATE_PASS = 7;

    /**
     * 坦克总数
     */
    public static final int ENEMY_MAX_COUNT = 8;
    /**
     * 间隔时间，产生坦克
     */
    public static final int ENEMY_BORN_INTERVAL = 1500;
    /**
     * 间隔时间，坦克随机移动或站立
     */
    public static final int ENEMY_AI_INTERVAL = 1500;
    /**
     * 开火概率
     */
    public static final double ENEMY_FIRE_PERCENT = 0.03;

    private static final String[] NAMES = {
            "行人", "乐园", "花草", "人才", "左手", "目的", "课文", "优点", "年代", "灰尘",
            "沙子", "小说", "儿女", "难题", "明星", "本子", "彩色", "水珠", "路灯", "把握",
            "房屋", "心愿", "左边", "新闻", "早点",
            "市场", "雨点", "细雨", "书房", "毛巾", "画家", "元旦", "绿豆", "本领", "起点",
            "青菜", "土豆", "总结", "礼貌", "右边",
            "老虎", "老鼠", "猴子", "树懒", "斑马", "小狗", "狐狸", "狗熊", "黑熊",
            "大象", "豹子", "麝牛", "狮子", "熊猫", "疣猪", "羚羊", "驯鹿", "考拉",
            "犀牛", "猞猁", "猩猩", "海牛", "水獭", "海豚", "海象", "刺猬", "袋鼠",
            "犰狳", "河马", "海豹", "海狮", "蝙蝠", "白虎", "狸猫", "水牛", "山羊",
            "绵羊", "牦牛", "猿猴", "松鼠", "野猪", "豪猪", "麋鹿", "花豹", "野狼",
            "灰狼", "蜂猴", "熊猴", "叶猴", "紫貂", "貂熊", "熊狸", "云豹", "雪豹",
            "黑麂", "野马", "鼷鹿", "坡鹿", "豚鹿", "野牛", "藏羚", "河狸", "驼鹿",
            "黄羊", "鬣羚", "斑羚", "岩羊", "盘羊", "雪兔"
    };

    private static final String[] MODIFY = {
            "可爱", "傻傻", "萌萌", "羞羞", "笨笨", "呆呆", "美丽", "聪明", "伶俐", "狡猾",
            "胖乎乎", "粉嫩嫩", "白胖胖", "漂亮", "可爱", "聪明", "懂事", "乖巧", "淘气",
            "淘气", "顽劣", "调皮", "顽皮", "天真", "可爱", "无邪", "单纯", "纯洁", "无暇",
            "纯真", "稚气", "温润", "好奇"
    };

    public static String getRandomName() {
        return MODIFY[RandomUtil.randomInt(MODIFY.length)] + NAMES[RandomUtil.randomInt(NAMES.length)];
    }

    public static Color getRandomColor() {
        int red = RandomUtil.randomInt(0, 256);
        int green = RandomUtil.randomInt(0, 256);
        int blue = RandomUtil.randomInt(0, 256);
        return new Color(red, green, blue);
    }

    public static boolean isTouch(int rectX, int rectY, int radius, int pointX, int pointY) {
        int absX = Math.abs(rectX - pointX);
        int absY = Math.abs(rectY - pointY);
        return absX < radius && absY < radius;
    }

    /**
     * 判断坦克是否重叠
     */
    public static void isTouch(List<Tank> tanks) {
        for (int i = 0; i < tanks.size(); i++) {
            Tank tank1 = tanks.get(i);
            for (int j = i + 1; j < tanks.size(); j++) {
                Tank tank2 = tanks.get(j);
                if (tank1.getRect().intersects(tank2.getRect())) {
                    tank1.back();
                    tank2.back();
                }
            }
        }
    }

    /**
     * 判断坦克出生是否重叠
     */
    public static boolean isBornTouch(List<Tank> tanks, int x, int y) {
        Tank tank = new Tank(x, y, 0, null);
        for (int i = 0; i < tanks.size(); i++) {
            Tank tank1 = tanks.get(i);
            if (tank.getRect().intersects(tank1.getRect())) {
                return true;
            }
        }
        return false;
    }

    public static Image getImage(String src) {
        return Toolkit.getDefaultToolkit().getImage(MyUtil.class.getResource(src));
    }
}

class Audio {
    private static AudioClip start;
    private static AudioClip fire;
    private static AudioClip bomb;

    static {
        start = Applet.newAudioClip(Audio.class.getResource("/static/start.wav"));
        fire = Applet.newAudioClip(Audio.class.getResource("/static/tank_fire.wav"));
        bomb = Applet.newAudioClip(Audio.class.getResource("/static/explode.wav"));
    }

    public static void playStart() {
        start.play();
    }

    public static void playFire() {
        fire.play();
    }

    public static void playBomb() {
        bomb.play();
    }
}